//
//  XTXHomeViewController.m
//  陪我V聊
//
//  Created by JackChen on 2018/8/7.
//  Copyright © 2018年 wzb. All rights reserved.
//

#import "XTXHomeViewController.h"

#import "MBProgressHUD.h"
#import "MJRefresh.h"
#import "XTXHomeCell.h"
#import "XTXXQViewController.h"

@interface XTXHomeViewController ()<UICollectionViewDataSource, UICollectionViewDelegate> {
    MBProgressHUD *HUD;
    NSInteger pageCount;
}
@property (nonatomic,strong) NSMutableArray *homeArr;
@property (nonatomic, weak) UICollectionView *collectionView;
@property (nonatomic, weak) UIImageView *wuneirongImageView;
@property (nonatomic, weak) UILabel *wuneirongLabel;

@end
static NSString * const ID = @"XTXHomeViewController";
@implementation XTXHomeViewController

-(NSMutableArray *)homeArr{
    if (_homeArr==nil) {
        _homeArr=[NSMutableArray array];
    }
    return _homeArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    pageCount = 1;
    self.view.backgroundColor = rgbYanse1(0xf5f5f5);
    [self chushihuaInterface];
}

-(void)chushihuaInterface{
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    CGFloat wh=(pingmuKuan-1)/2;
    layout.itemSize=CGSizeMake(wh, wh);
    layout.minimumLineSpacing=1;
    layout.minimumInteritemSpacing=1;
    UICollectionView *collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, pingmuKuan, pingmuGao-zhuangtaiDaohangGao-self.tabBarController.tabBar.frame.size.height) collectionViewLayout:layout];
    [self.view addSubview:collectionView];
    collectionView.showsVerticalScrollIndicator=NO;
    collectionView.showsHorizontalScrollIndicator=NO;
    collectionView.dataSource=self;
    collectionView.delegate=self;
    [collectionView registerClass:XTXHomeCell.class forCellWithReuseIdentifier:ID];
    _collectionView=collectionView;
    _collectionView.backgroundColor=UIColor.clearColor;
    // 刷新加载数据
    _collectionView.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self->pageCount ++;
        [self shouyedewangluoqingqiujiazai];
    }];
    _collectionView.mj_footer.hidden=YES;
    _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self->pageCount = 1;
        [self shouyedewangluoqingqiujiazai];
    }];
    [_collectionView.mj_header beginRefreshing];
    UILabel *wuneirongLabel = [[UILabel alloc]init];
    wuneirongLabel.font = [UIFont systemFontOfSize:14];
    wuneirongLabel.textColor = rgbYanse1(0xc8c8c8);
    wuneirongLabel.textAlignment = NSTextAlignmentCenter;
    wuneirongLabel.text = @"暂无内容";
    [self.view addSubview:wuneirongLabel];
    _wuneirongLabel=wuneirongLabel;
    UIImageView *wuneirongImageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"无内容图片"]];
    [self.view addSubview:wuneirongImageView];
    _wuneirongImageView=wuneirongImageView;
    [_wuneirongImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.centerY.equalTo(self.view.mas_centerY);
    }];
    [_wuneirongLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(wuneirongImageView.mas_bottom).offset(20);
    }];
    _wuneirongLabel.hidden=YES;
    _wuneirongImageView.hidden=YES;
}

- (void)shouyedewangluoqingqiujiazai
{
    NSMutableDictionary *tempDict = [NSMutableDictionary dictionary];
    tempDict[@"user_id"] = XTXGongTool.share.useMode.id;
    tempDict[@"page"] = [NSString stringWithFormat:@"%ld",(long)pageCount];
    tempDict[@"sex"] = XTXGongTool.share.useMode.sex;
    if (self.pageIndex == 0) {
        [self huoquguanzhujiemiandewangluoqingqiu:tempDict.copy];
    }
    else if (self.pageIndex == 1) {
        tempDict[@"pack"] = APPLink;
        [self huoqutuijianjiemiandewangluoqingqiuEventClick:tempDict.copy];
    }
    else if (self.pageIndex == 2) {
        tempDict[@"pack"] = APPLink;
        [self huoquhuoyuejiemiandewangluoqingqiuEventClick:tempDict.copy];
    }
}

- (void)huoquguanzhujiemiandewangluoqingqiu:(NSDictionary *) tempDict
{
    WeakSelf
    [XTXNetworking networkingGetUseGuanzuListdewangluoqingqiu:tempDict completionHandler:^(NSDictionary * _Nonnull dict) {
        if ([dict[@"code"] integerValue] == 1) {
            if (self->pageCount == 1) {
                [weakSelf.homeArr removeAllObjects];
            }
            NSMutableArray *arr=[ConsumerModel mj_objectArrayWithKeyValuesArray:dict[@"info"]];
            if (arr.count>0) {
                [self.homeArr addObjectsFromArray:arr];
            }
            [weakSelf.collectionView reloadData];
            [weakSelf.collectionView.mj_header endRefreshing];
            [weakSelf.collectionView.mj_footer endRefreshing];
            [self collectionViewFooterView];
        }else {
            [XTXGongView showTipMsg:dict[@"msg"]];
        }
    }];
}

- (void)huoqutuijianjiemiandewangluoqingqiuEventClick:(NSDictionary *) tempDict
{
    WeakSelf
    [XTXNetworking networkingGetUseTuijianListdewangluoqingqiu:tempDict completionHandler:^(NSDictionary * _Nonnull dict) {
        if ([dict[@"code"] integerValue] == 1) {
            if (self->pageCount == 1) {
                [weakSelf.homeArr removeAllObjects];
            }
            NSMutableArray *arr=[ConsumerModel mj_objectArrayWithKeyValuesArray:dict[@"info"]];
            if (arr.count>0) {
                [self.homeArr addObjectsFromArray:arr];
            }
            [weakSelf.collectionView reloadData];
            [weakSelf.collectionView.mj_header endRefreshing];
            [weakSelf.collectionView.mj_footer endRefreshing];
            [self collectionViewFooterView];
        }else {
            [XTXGongView showTipMsg:dict[@"msg"]];
        }
    }];
}

- (void)huoquhuoyuejiemiandewangluoqingqiuEventClick:(NSDictionary *) tempDict
{
    WeakSelf
    [XTXNetworking networkingGetUseHuoyueListdewangluojiekou:tempDict completionHandler:^(NSDictionary * _Nonnull dict) {
        if ([dict[@"code"] integerValue] == 1) {
            if (self->pageCount == 1) {
                [weakSelf.homeArr removeAllObjects];
            }
            NSMutableArray *arr=[ConsumerModel mj_objectArrayWithKeyValuesArray:dict[@"info"]];
            if (arr.count>0) {
                [self.homeArr addObjectsFromArray:arr];
            }
            [weakSelf.collectionView reloadData];
            [weakSelf.collectionView.mj_header endRefreshing];
            [weakSelf.collectionView.mj_footer endRefreshing];
            [self collectionViewFooterView];
        }else {
            [XTXGongView showTipMsg:dict[@"msg"]];
        }
    }];
}
///判断是否显示
-(void)collectionViewFooterView{
    if (_collectionView.collectionViewLayout.collectionViewContentSize.height>=pingmuGao) {
        _collectionView.mj_footer.hidden=NO;
    }else{
        _collectionView.mj_footer.hidden=YES;
    }
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (_homeArr.count==0) {
        _wuneirongLabel.hidden=NO;
        _wuneirongImageView.hidden=NO;
    }else{
        _wuneirongLabel.hidden=YES;
        _wuneirongImageView.hidden=YES;
    }
    return _homeArr.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    XTXHomeCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.consumerModel=_homeArr[indexPath.item];
    return cell;
}
#pragma mark - UICollectionViewDelegate
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.homeArr.count > indexPath.item && [[self.homeArr[indexPath.item] id] length]) {
        XTXXQViewController *infoView = [[UIStoryboard storyboardWithName:@"Info" bundle:nil] instantiateViewControllerWithIdentifier:NSStringFromClass(XTXXQViewController.class)];
        infoView.hidesBottomBarWhenPushed = YES;
        infoView.userID = [self.homeArr[indexPath.row] id];
        [self.navigationController pushViewController:infoView animated:YES];
    }
}
@end
